{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8e15564b-cc51-4dc3-8065-553913338ff8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "# 选择图片\n",
    "# 1 2 3\n",
    "target = 1\n",
    "\n",
    "if target==1:\n",
    "    data_name = '0618'\n",
    "elif target==2:\n",
    "    data_name = '0854'\n",
    "elif target==3:\n",
    "    data_name = '1066'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea5355b1-ba26-43fa-9ff5-cde3fc85cf83",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据暂存\n",
    "features = []\n",
    "labels = []\n",
    "# 用于决定采集的数据标签\n",
    "# 0未标记 1road 2usually tree 3sky\n",
    "current_label = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de737d05-294f-415c-a1f5-bf1b6748ad4e",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x5565299b1c30) is not the object's thread (0x556529aa2880).\n",
      "Cannot move to target thread (0x5565299b1c30)\n",
      "\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[2], line 28\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m     26\u001b[0m     cv2\u001b[38;5;241m.\u001b[39mimshow(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mimage\u001b[39m\u001b[38;5;124m'\u001b[39m, img)\n\u001b[0;32m---> 28\u001b[0m     key \u001b[38;5;241m=\u001b[39m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwaitKey\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;241m&\u001b[39m \u001b[38;5;241m0xFF\u001b[39m\n\u001b[1;32m     30\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m key \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mord\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m1\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[1;32m     31\u001b[0m         \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCollecting samples for class 1. Press \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m2\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m or \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m3\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m to switch, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mEsc\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m to stop.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# 鼠标回调函数\n",
    "def on_mouse(event, x, y, flags, param):\n",
    "    global features, labels, is_positive\n",
    "\n",
    "    if event == cv2.EVENT_LBUTTONDOWN:\n",
    "        color = img[y, x] # 虽然是img[y, x], 但是返回的是BGR三个通道的数值\n",
    "\n",
    "        features.append(color)\n",
    "        labels.append(current_label)\n",
    "        print(f'Collected color: {color} at position: {(x, y)} with label: {current_label}')\n",
    "### 开启新窗口\n",
    "cv2.namedWindow('image')\n",
    "cv2.setMouseCallback(\"image\", on_mouse)\n",
    "# 加载图片\n",
    "img_path = f'./input_data/{data_name}.png'\n",
    "img = cv2.imread(img_path)\n",
    "img_name = os.path.splitext(os.path.basename(img_path))[0]\n",
    "\n",
    "while True:\n",
    "    cv2.imshow('image', img)\n",
    "\n",
    "    key = cv2.waitKey(1) & 0xFF\n",
    "\n",
    "    if key == ord('1'):\n",
    "        print(\"Collecting samples for class 1. Press '2' or '3' to switch, 'Esc' to stop.\")\n",
    "        current_label = 1\n",
    "    elif key == ord('2'):\n",
    "        print(\"Collecting samples for class 2. Press '1' or '3' to switch, 'Esc' to stop.\")\n",
    "        current_label = 2\n",
    "    elif key == ord('3'):\n",
    "        print(\"Collecting samples for class 3. Press '1' or '2' to switch, 'Esc' to stop.\")\n",
    "        current_label = 0\n",
    "    elif key == 27:  # Esc key\n",
    "        print(\"Stopping data collection.\")\n",
    "        break\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7e3601c-37a3-46cc-b15d-cda50b7c1426",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关闭窗口\n",
    "cv2.destroyAllWindows()\n",
    "# 转为numpy数组处理\n",
    "features = np.array(features)\n",
    "labels = np.array(labels)\n",
    "\n",
    "# 数据保存到 CSV 文件的函数\n",
    "def save_to_csv(features, labels, filename):\n",
    "    # 检查文件是否存在\n",
    "    if os.path.exists(filename):\n",
    "        # 读取现有的 CSV 文件\n",
    "        existing_data = pd.read_csv(filename)\n",
    "        # 将新数据转换为 DataFrame\n",
    "        new_data = pd.DataFrame({\n",
    "            'B': features[:, 0],\n",
    "            'G': features[:, 1],\n",
    "            'R': features[:, 2],\n",
    "            'Label': labels\n",
    "        })\n",
    "        # 合并数据\n",
    "        combined_data = pd.concat([existing_data, new_data], ignore_index=True)\n",
    "        # 保存合并后的数据\n",
    "        combined_data.to_csv(filename, index=False)\n",
    "    else:\n",
    "        # 如果文件不存在，直接保存新数据\n",
    "        new_data = pd.DataFrame({\n",
    "            'B': features[:, 0],\n",
    "            'G': features[:, 1],\n",
    "            'R': features[:, 2],\n",
    "            'Label': labels\n",
    "        })\n",
    "        new_data.to_csv(filename, index=False)\n",
    "\n",
    "# 采集完数据后，调用 save_to_csv 函数保存数据\n",
    "csv_filename = f'./RGB_data/data_{img_name}_multi.csv'\n",
    "save_to_csv(features, labels, csv_filename)\n",
    "\n",
    "print(f'Data has been saved to {csv_filename}')\n",
    "# 绘制R-G图\n",
    "def plot_RGB(features, labels):\n",
    "    # 分离三个类别的样本\n",
    "    label_0_features = features[labels == 0]\n",
    "    label_1_features = features[labels == 1]\n",
    "    label_2_features = features[labels == 2]\n",
    "    \n",
    "    # OpenCV 默认提取通道是BGR -> B(0), G(1), R(2)\n",
    "    label_0_R = label_0_features[:, 2]\n",
    "    label_0_G = label_0_features[:, 1]\n",
    "    \n",
    "    label_1_R = label_1_features[:, 2]\n",
    "    label_1_G = label_1_features[:, 1]\n",
    "    \n",
    "    label_2_R = label_2_features[:, 2]\n",
    "    label_2_G = label_2_features[:, 1]\n",
    "    \n",
    "    # 绘制每个类别的样本\n",
    "    plt.scatter(label_0_R, label_0_G, color='red', label='Label 0 Samples')\n",
    "    plt.scatter(label_1_R, label_1_G, color='green', label='Label 1 Samples')\n",
    "    plt.scatter(label_2_R, label_2_G, color='blue', label='Label 2 Samples')\n",
    "    \n",
    "    plt.title(f'R-G Channel Plot of {img_name}')\n",
    "    plt.xlabel('R Channel')\n",
    "    plt.ylabel('G Channel')\n",
    "    plt.legend()\n",
    "    plt.show()\n",
    "\n",
    "# 调用函数绘制R-G图\n",
    "plot_RGB(features, labels)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
